//
//  LXTextFieldCell.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/14.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXTextFieldCell.h"

@interface LXTextFieldCell()<UITextFieldDelegate>

@property(nonatomic,strong)UILabel * titleLabel;

@property(nonatomic,strong)UIView * underLinde;

@end

@implementation LXTextFieldCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.cellHeight = 50;
        UITextField * textField = [[UITextField alloc] init];
        textField.backgroundColor = [UIColor clearColor];
        textField.borderStyle = UITextBorderStyleNone;
        textField.clearsOnBeginEditing = YES;
        textField.delegate = self;
        textField.clearButtonMode = UITextFieldViewModeAlways;
        
        textField.delegate = self;
        
        
        self.textField = textField;
        textField.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:textField];
        textField.textAlignment = NSTextAlignmentRight; //水平左对齐
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  //垂直居
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.textColor = HexColor(@"#333333");
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(globalMargin, 0, ScreenWidth-2*globalMargin, self.cellHeight)];
        [self.contentView insertSubview:imageView atIndex:0];
        self.backgroundImageView = imageView;
        
        UIView * underLinde = [[UIView alloc] initWithFrame:CGRectMake(2*globalMargin, self.cellHeight-1, ScreenWidth-4*globalMargin, 1)];
        underLinde.backgroundColor = HexColor(@"#EEEEEE");
        [self.contentView addSubview:underLinde];
        self.underLinde = underLinde;
        self.enableUnderLine = NO;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];

    self.titleLabel.width = [self.titleLabel.text sizeInAre:MaxSize font:self.titleLabel.font].width;
    self.titleLabel.height = [self.titleLabel.text sizeInAre:MaxSize font:self.titleLabel.font].height;
    self.titleLabel.x = 20;
    self.titleLabel.centerY = self.cellHeight * 0.5;
    

    self.textField.height = self.cellHeight * 0.5;
    self.textField.x = self.titleLabel.right + 10;
    self.textField.centerY = self.cellHeight*0.5;
    self.textField.width = ScreenSize.width - self.titleLabel.right - 2*globalMargin;
    self.textField.text = @"";
}


-(void)setBackgroundImage:(UIImage *)backgroundImage{
    _backgroundImage = backgroundImage;
    self.backgroundImageView.image = _backgroundImage;
    [self setNeedsLayout];
}


-(void)setEnableUnderLine:(BOOL)enableUnderLine{
    _enableUnderLine = enableUnderLine;
    self.underLinde.hidden = !_enableUnderLine;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if([self.delegate respondsToSelector:@selector(LXTextFieldCell:DidEndEditing:)]){
        [self.delegate LXTextFieldCell:self DidEndEditing:textField.text];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}
@end
